package ch.pizzapp.kurier;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import ch.pizzapp.kurier.adapter.NotificationAdapter;
import ch.pizzapp.kurier.client.InfoClient;
import ch.pizzapp.kurier.data.NoficationResponceData;
import ch.pizzapp.kurier.data.NotificationData;
import ch.pizzapp.kurier.dto.InfoDto;

/**
 * Created by shanuka on 12/9/13.
 */
@EFragment(R.layout.notification_activity)
public class NotificationFragment extends Fragment {

	@ViewById(R.id.login_status)
	protected LinearLayout mLoginStatusView;

	@Bean
	InfoDto infoDto;
	@RestService
	InfoClient InfoClient;

	@Bean
	NotificationAdapter notificationAdapter;

	@ViewById(R.id.listViewNotification)
	ListView listViewNotification;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (savedInstanceState == null) {
			getInfo();
		}
		return null;

	}

	@AfterViews
	@Background
	void getInfo() {

		infoDto.setType("17");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setInfor(InfoClient.getNotification(infoDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("Not connected ");
			}
		});
	}

	@ItemClick({ R.id.listViewNotification })
	void itemOnClikc(NotificationData notificationData) {
		NotificationDetailFragment fragment = new NotificationDetailFragment_();

		Bundle args = new Bundle();
		args.putParcelable("NotificationData", notificationData);

		fragment.setArguments(args);

		final FragmentTransaction transaction = getActivity()
				.getSupportFragmentManager().beginTransaction();
		transaction.add(R.id.container, fragment);
		transaction.addToBackStack(null);
		transaction.commit();
	}

	@UiThread
	void setInfor(final NoficationResponceData noficationResponceData) {

		// System.out.println(noficationResponceData.getResponse().getAddress1());

		showProgress(false);

		if (noficationResponceData != null) {
			List<NotificationData> notificationDataList = noficationResponceData
					.getResponse().getNotificationDataList();

			notificationAdapter.setNotificationDataData(notificationDataList);

			listViewNotification.setAdapter(notificationAdapter);
		}

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	@Click({ R.id.loginButton })
	void onClick(View view) {
		switch (view.getId()) {
		case R.id.loginButton:
			openDialog();
			break;
		default:
			break;

		}
	}

	@Trace
	private void openDialog() {
		final Dialog dialog = new Dialog(getActivity());
		dialog.setTitle("login");
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.login_dialog);
		dialog.getWindow().setBackgroundDrawable(
				new ColorDrawable(android.graphics.Color.TRANSPARENT));
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

		dialog.getWindow().setGravity(Gravity.BOTTOM);
		Button btnDismiss = (Button) dialog.getWindow().findViewById(
				R.id.cancelButton);

		btnDismiss.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	@UiThread
	void showProgress(final boolean show) {
		mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
		listViewNotification.setVisibility(show ? View.GONE : View.VISIBLE);
	}
}